/*
 * Store.java
 * Team qq 2011
 */
package com.google.code.timetrail.backend;

/**
 * implements the Stores that will be used in the trail
 * 
 * @author Steven
 * @version 1.0 As of now stores have unlimited inventory but that might change
 *          in the near future
 * 
 *          The way this works is you do all the displaying and stuff with the
 *          temporary values then when next is clicked you run it all 4 REALZ
 *          and then commit the inventory back to controller
 */
public class Store {
	/**
	 * playerInv - the player's inventory.
	 */
	private final Inventory playerInv;

	/**
	 * tempHullParts - the temporary number of hull parts.
	 */
	private int tempHullParts;

	/**
	 * tempFood - the temporary number of food.
	 */
	private int tempFood;

	/**
	 * tempFuelCells - the temporary number of fuel cells.
	 */
	private int tempFuelCells;

	/**
	 * tempMoney - the temporary number of moneys.
	 */
	private int tempMoney;

	/**
	 * tempTimeSuits - the temporary number of time suits.
	 */
	private int tempTimeSuits;

	/**
	 * tempTimeBulletBills - the temporary number of time bullet bills.
	 */
	private int tempTimeBulletBills;

	/**
	 * tempEngines - the temporary number of engines.
	 */
	private int tempEngines;

	/**
	 * tempFCapacitors - the temporary number of fuel capacitors.
	 */
	private int tempFCapacitors;

	/**
	 * engines - the instance of Engine.
	 */
	private final Item engines;

	/**
	 * fCapacitors - the Instance of Flux Capacitors.
	 */
	private final Item fCapacitors;

	/**
	 * hullParts - the Instance of hull parts.
	 */
	private final Item hullParts;

	/**
	 * food - the instance of food.
	 */
	private final Item food;

	/**
	 * fuelCells - the instance of fuel cells.
	 */
	private final Item fuelCells;

	/**
	 * money - the instance of money.
	 */
	private final Item money;

	/**
	 * timeBulletBills - the instance of time bullet bills.
	 */
	private final Item timeBulletBills;

	/**
	 * timeSuits - the instance of time suits.
	 */
	private final Item timeSuits;

	/**
	 * The Control for the store
	 */
	private final Control cont;

	/**
	 * Value VALUE_HUNDRED
	 */
	private static final int VALUE_HUNDRED = 100;

	/**
	 * Position POSITION_TWO
	 */
	private static final int POSITION_TWO = 2;

	/**
	 * sets the temporary item values to zero, creates instances of all
	 * inventory items.
	 * 
	 * @param cont
	 *            the Control instance that holds the game data
	 */
	public Store(Control cont) {
		this.cont = cont;
		playerInv = cont.getInv();
		tempMoney = 0;
		tempFuelCells = 0;
		tempFood = 0;
		tempHullParts = 0;
		tempFCapacitors = 0;
		tempEngines = 0;

		money = new Money();
		fuelCells = new FuelCell();
		food = new Food();
		hullParts = new HullPart();
		fCapacitors = new FluxCapacitor();
		engines = new Engine();
		timeSuits = new TimeSuit();
		timeBulletBills = new TimeBulletBill();
	}

	/**
	 * adds an item to the inventory while factoring in cost PRECONDITION:THE
	 * MONEY IS NOT AN ISSUE
	 * 
	 * @param amount
	 *            the amount of the item to buy
	 * @param item
	 *            the item to buy
	 */
	public void buyItem(int amount, Item item) {
		if (canBuyItem(amount, item)) {
			playerInv.addItem(amount, item);

			// TODO Make this section work
			playerInv
					.getMoney()
					.removeCurrentAmount(
							amount
									* ((item.getValue() - (item.getValue() * cont
											.getPlayer().getSkills()[POSITION_TWO])) / VALUE_HUNDRED));
		}

	}

	/**
	 * Checks if buying an item is viable DOES NOT BUY AN ITEM
	 * 
	 * @param amount
	 *            the amount of the item you want to buy
	 * @param item
	 *            the item you want to buy
	 * @return true if the item was bought
	 */
	public boolean canBuyItem(int amount, Item item) {
		final boolean itemCheck = playerInv.canAddItem(amount, item);
		final boolean moneyCheck = playerInv.getMoney().canRemoveCurrentAmount(
				amount
						* (item.getValue() - ((item.getValue() * cont
								.getPlayer().getSkills()[POSITION_TWO]))
								/ VALUE_HUNDRED));

		return itemCheck && moneyCheck;
	}

	/**
	 * Possibility to add soon, currently not in use PRECONDITION MONEY IS NOT
	 * AN ISSUE removes an item from the inventory while also giving the player
	 * some money
	 * 
	 * @param amount
	 *            amount of items to be removed from the inventory
	 * @param item
	 *            the type of item to remove
	 * 
	 * @return removeItem's return unless the other inventory has insufficient
	 *         money
	 */
	@Deprecated
	public boolean canSellItem(int amount, Item item) {
		return false;
	}

	/**
	 * commits the players inventory to what was bought in the store
	 * 
	 * @param cont
	 *            the instance of Control that holds the game information
	 */
	public void commitInventoryChanges(Control cont) {
		cont.setInv(playerInv);
	}

	/**
	 * getter for tempMoney
	 * 
	 * @return tempMoney
	 */
	public int getTempMoney() {
		return tempMoney;
	}

	/**
	 * setter for tempMoney
	 * 
	 * @param tempMoney
	 */
	public void setTempMoney(int tempMoney) {
		this.tempMoney = tempMoney;
	}

	/**
	 * getter for tempFuelCells
	 * 
	 * @return tempFuelCells
	 */
	public int getTempFuelCells() {
		return tempFuelCells;
	}

	/**
	 * setter for tempFuelCells
	 * 
	 * @param tempFuelCells
	 */
	public void setTempFuelCells(int tempFuelCells) {
		this.tempFuelCells = tempFuelCells;
	}

	/**
	 * getter for TempFood
	 * 
	 * @return tempFood
	 */
	public int getTempFood() {
		return tempFood;
	}

	/**
	 * setter for tempFood
	 * 
	 * @param tempFood
	 */
	public void setTempFood(int tempFood) {
		this.tempFood = tempFood;
	}

	/**
	 * getter for TempHullParts
	 * 
	 * @return tempHullParts
	 */
	public int getTempHullParts() {
		return tempHullParts;
	}

	/**
	 * setter for TempHullParts
	 * 
	 * @param tempHullParts
	 */
	public void setTempHullParts(int tempHullParts) {
		this.tempHullParts = tempHullParts;
	}

	/**
	 * getter for tempFCapacitors
	 * 
	 * @return tempFCapacitors
	 */
	public int getTempFCapacitors() {
		return tempFCapacitors;
	}

	/**
	 * setter for tempFCapacitors
	 * 
	 * @param tempFCapacitors
	 */
	public void setTempFCapacitors(int tempFCapacitors) {
		this.tempFCapacitors = tempFCapacitors;
	}

	/**
	 * getter for tempEngines
	 * 
	 * @return tempEngines
	 */
	public int getTempEngines() {
		return tempEngines;
	}

	/**
	 * setter for tempEngines
	 * 
	 * @param tempEngines
	 */
	public void setTempEngines(int tempEngines) {
		this.tempEngines = tempEngines;
	}

	/**
	 * setter for tempTimeBulletBills.
	 * 
	 * @param tempTimeBulletBills
	 */
	public void setTempTimeBulletBills(int tempTimeBulletBills) {
		this.tempTimeBulletBills = tempTimeBulletBills;
	}

	/**
	 * setter for TempTimeSuits.
	 * 
	 * @param tempTimeSuits
	 */
	public void setTempTimeSuits(int tempTimeSuits) {
		this.tempTimeSuits = tempTimeSuits;
	}

	/**
	 * sums up the current temporary weight added to the actual weight
	 * 
	 * @return the sum of all the temp weights added to the current weight
	 */
	public int getTempWeight() {
		return playerInv.getInvWeight() + tempMoney * money.getWeight()
				+ tempFuelCells * fuelCells.getWeight() + tempFood
				* food.getWeight() + tempHullParts * hullParts.getWeight()
				+ tempFCapacitors * fCapacitors.getWeight() + tempEngines
				* engines.getWeight() + tempTimeSuits * timeSuits.getWeight()
				+ tempTimeBulletBills * timeBulletBills.getWeight();
	}

	/**
	 * Sums the balance temporarily for gui purposes
	 * 
	 * @return the summation of the temporary variables minus the current money
	 *         amount
	 */
	public int getTempCost() {
		return playerInv.getMoney().getCurrentAmount()
				- ((tempMoney * (money.getValue() - ((money.getValue() * cont
						.getPlayer().getSkills()[POSITION_TWO]))
						/ VALUE_HUNDRED)) // is money
						// factored in
						// by bargaining
						// skill?
						+ (tempFuelCells * (fuelCells.getValue() - ((fuelCells
								.getValue() * cont.getPlayer().getSkills()[POSITION_TWO]))
								/ VALUE_HUNDRED))
						+ (tempFood * (food.getValue() - ((food.getValue() * cont
								.getPlayer().getSkills()[POSITION_TWO]))
								/ VALUE_HUNDRED))
						+ (tempHullParts * (hullParts.getValue() - ((hullParts
								.getValue() * cont.getPlayer().getSkills()[POSITION_TWO]))
								/ VALUE_HUNDRED))
						+ (tempFCapacitors * (fCapacitors.getValue() - ((fCapacitors
								.getValue() * cont.getPlayer().getSkills()[POSITION_TWO]))
								/ VALUE_HUNDRED))
						+ (tempEngines * (engines.getValue() - ((engines
								.getValue() * cont.getPlayer().getSkills()[POSITION_TWO]))
								/ VALUE_HUNDRED))
						+ (tempTimeSuits * (timeSuits.getValue() - ((timeSuits
								.getValue() * cont.getPlayer().getSkills()[POSITION_TWO]))
								/ VALUE_HUNDRED)) + (tempTimeBulletBills * (timeBulletBills
						.getValue() - ((timeBulletBills.getValue() * cont
						.getPlayer().getSkills()[POSITION_TWO]))
						/ VALUE_HUNDRED)));
	}

	/**
	 * returns the string representation of the store
	 * 
	 * @return the string representation of the store
	 */
	public String toString() {
		return "Store";
	}

}
